package bimvu.lab21;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import bimvu.lab21.R;

public class DownloadedContentActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloaded_content);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            Object object = bundle.get("url");
            try {
                String url = (String) object;
                UrlDownloaderTask task = new UrlDownloaderTask(this);
                task.execute(url);
            } catch (NullPointerException e) {
            } catch (ClassCastException e) {
            }
        }
    }

    public void updateDownloadedContentView(Bitmap bitmap, long size, float averageSpeed)
    {
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);
        TextView downloadedSizeTextView = (TextView) findViewById(R.id.downloadedSize);
        downloadedSizeTextView.setText(Long.toString(size) + " Bytes");
        TextView averageSpeedTextView = (TextView) findViewById(R.id.averageSpeed);
        averageSpeedTextView.setText(Float.toString(averageSpeed) + " KB/s");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.downloaded_content, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
